// ValidationAspects
// Written by Mike Saunders
// Released under Microsoft Public License (Ms-PL)
using System;
using ValidationAspects.Sdk;

namespace ValidationAspects.Exceptions
{
	public class ValidateMatchesRegexException : ValidationException
	{
		public static Func<ValidateMatchesRegexException, string> MessageFormat { get; set; }

		static ValidateMatchesRegexException()
		{
			ResetMessageFormat();
		}

		public ValidateMatchesRegexException(string regularExpressionPattern, string valueToMatch)
		{
			Enforce.IsNotNullOrEmpty(regularExpressionPattern, "regularExpressionPattern");
			Pattern = regularExpressionPattern;
			Value = valueToMatch;
		}

		public static void ResetMessageFormat()
		{
			MessageFormat = e =>
			                {
			                	string validationTargetName = e.GetValidationTargetName();

			                	if (!string.IsNullOrEmpty(e.Name))
			                		return string.Format("{0} is not a valid {1}.", validationTargetName, e.Name);

			                	return string.Format("{0} does not match Regex[{1}].", validationTargetName, e.Pattern);
			                };
		}

		public override string Message
		{
			get { return MessageFormat(this); }
		}

		public string Pattern { get; private set; }
		public string Value { get; private set; }
		public string Name { get; set; }
	}
}